-module(san_gong_fight_record).

-include("common.hrl").
-export([insert_fighting_record_key/2,test/0,update_sg_fighting_record/9,req_get_fightting_record_info/2,req_get_every_times_info/3
		]).

-define(ROOM_OWNER,1).

insert_fighting_record_key(ID,List) ->
	Fun = fun({Aid,_}) ->
				  case db:dirty_get(fighting_record_key, ID, #fighting_record_key.fighting_record_id) of
					  [#fighting_record_key{}] -> skip;
					  _ -> db:insert(#fighting_record_key{fighting_record_id=ID,aid=Aid})
				  end
		  end,
	lists:foreach(Fun, List).

update_sg_fighting_record(Room_owner,_ZhuangAid,Room_id,Room_type,Room_mode,Room_score,Game_times,Fighting_record,Jiesuan_score_list) ->
	check_old_fighting_record(Jiesuan_score_list),
	Player_record =  get_player_info(Room_owner,Jiesuan_score_list),
	Fighting_record = #fighting_record_info{room_id=Room_id,game_times=Game_times,room_type=Room_type,room_mode =Room_mode,
											under_score=Room_score,last_times=util:unixtime(),
											player_record=util:to_binary(Player_record)},
	case Fighting_record of
		[#fighting_record_info{id=ID}] -> insert_fighting_record_key(ID,Jiesuan_score_list),
										  insert_every_times_info(ID,Fighting_record);
		_ -> skip
	end.

check_old_fighting_record(Jiesuan_score_list) ->
	Fun= fun({Aid,_}) ->
				case db:dirty_get(fighting_record_key, Aid, #fighting_record_key.aid) of
					 List when length(List)+1 > 10 ->delete_old(List);
					_ -> skip
				end
		 end,
	lists:foreach(Fun, Jiesuan_score_list).

delete_old([#fighting_record_key{id=ID}| _Next]) -> db:delete(fighting_record_key, ID);
delete_old(_) -> error.

insert_every_times_info(ID,Fighting_record) -> 
	case db:dirty_get(fighting_every_times_info, ID,#fighting_every_times_info.fighting_record_id) of
		L when length(L) > 0 -> skip;
		_ ->Fun = fun({Inning,List}) ->
						  
						  case get_player_every_times_info(List,[]) of
							  [{One},{Two},{Three},{Four},{Five},{Six}] ->
								  db:insert(#fighting_every_times_info{fighting_record_id=ID,inning=Inning,one_info =util:to_binary(One),
																	   two_info=util:to_binary(Two),three_info=util:to_binary(Three),
																	   four_info=util:to_binary(Four),five_info=util:to_binary(Five),
																	    six_info=util:to_binary(Six)});
                         _ -> skip
                      end
                end,
          lists:foreach(Fun, Fighting_record)
	end.

get_player_every_times_info([],Getlist) when length(Getlist)  =/= 6 andalso length(Getlist) < 6 ->
	Surplus = 6 - length(Getlist),
	?log("Surplus....=~p",[Surplus]),
	L = lists:map(fun(_) -> {""} end, lists:seq(1, Surplus)),
	?log("L..........=,Getlist=~p",[L++Getlist]),
	L++Getlist;
get_player_every_times_info([{Aid,Name,Curscore,Stake,Cardlist,Dian_shu,Zhuangstate}| Next ],Getlist) ->
	L = {util:to_list(Aid),util:to_list(Name),util:to_list(Curscore),util:to_list(Stake),util:to_list(Cardlist),
		 util:to_list(Dian_shu),util:to_list(Zhuangstate)},
	L1 = erlang:tuple_to_list(L),
	StrList = string:join(L1, "&"),
	get_player_every_times_info(Next,[{StrList}]++Getlist).


get_player_info(Room_owner,Jiesuan_score_list) ->
	Fun = fun({Aid,Amountscore}) ->
				  Player_Name =
					  case db:dirty_get(account, Aid) of
						  [#account{name = Name}] ->Name;
						  _ -> ""
					  end,
				  Owner_state = if Aid == Room_owner -> ?ROOM_OWNER;
								   true -> 0
								end,
				  L = {util:to_list(Aid),util:to_list(Player_Name),util:to_list(Owner_state),util:to_list(Amountscore)},
				  L1 = erlang:tuple_to_list(L),
				  string:join(L1, "&")
				  
		  end,
	Strlist = lists:map(Fun, Jiesuan_score_list),
	string:join(Strlist, "|").

deserialize_player_info(List) ->
	Str_list=string:tokens(util:to_list(List), "|"),
	F=fun(Str)->
			  [Aid,Player_Name,Owner_state,Amountscore]=string:tokens(Str, "&"),					
			  list_to_tuple([util:to_integer(Aid),util:to_list(Player_Name),util:to_integer(Owner_state),util:to_integer(Amountscore)]) 
	  end,
	L = lists:map(F, Str_list),
	pack_player_info(L).

pack_player_info(L) ->
	Fun = fun({Aid,Player_Name,Owner_state,Amountscore}) ->
				  Pt =pt_public_class:player_fighting_list_new(),
				  Pt#pt_public_player_fighting_list{aid=Aid,name=Player_Name,room_owner=Owner_state,amount_score=Amountscore}
		  end,
	lists:map(Fun, L).

test() -> [{One},{Two},{Three},{Four},{Five},{Six}]  = get_player_every_times_info([{1,"a",2,3,[{1,1},{2,2}],4,1},{9,"b",8,7,[{9,9},{8,8}],6,0}],[]),
		  ?log("L5................=~p,m =~p",[Five,string:tokens(Five, "&")]),
		   [Aid,Name,Curscore,Stake,Cardlist,Dian_shu,Zhuangstate]=string:tokens(Five, "&"),		
		  ?log("aaaaaaaaaa"),
		  Lb= list_to_tuple([util:to_integer(Aid),util:to_list(Name),util:to_integer(Curscore),util:to_integer(Stake),
                            util:to_list(Cardlist),util:to_integer(Dian_shu),util:to_integer(Zhuangstate)]),
		  ?log("Lb...........=~p",[Lb]).
          
req_get_fightting_record_info(Sid,Aid) ->
	case db:dirty_get(fighting_record_key, Aid, #fighting_record_key.aid) of
		List when length(List) > 0 ->
			Fun = fun(#fighting_record_key{fighting_record_id =ID}) ->
						  Pt1 =pt_public_class:fighting_record_info_new(),
						  case db:dirty_get(fighting_record_info, ID,#fighting_record_info.id) of
							  [#fighting_record_info{room_id=Room_id,game_times=Game_times,room_type=Room_type,
													 room_mode =Room_mode,under_score=Under_score,last_times=Last_times,
													 player_record=Player_record}] ->										
								  Pt1#pt_public_fighting_record_info{room_id=Room_id,game_times=Game_times,under_score=Under_score,
																	 room_type=Room_type,ply_mode=Room_mode,last_time=Last_times,
																	 player_fighting_list=deserialize_player_info(Player_record),
																	 fighting_record_id=ID};
							  _ ->Pt1
						  end
				  end,
			Info  = lists:map(Fun, List),
			Pt = pt_sg_fighting_record_info_d106:new(),
			Pt2 = Pt#pt_sg_fighting_record_info{fighting_record_info=Info},
			?send(Sid,pt_sg_fighting_record_info_d106:to_binary(Pt2, 0));
		_ -> skip
	end.

req_get_every_times_info(Sid,_Aid,ID) ->
	case db:dirty_get(fighting_every_times_info, ID,#fighting_every_times_info.fighting_record_id) of 
          List when length(List) > 0 ->
			  Fun = fun(#fighting_every_times_info{inning=Inning,one_info =One_info,two_info=Two_info,three_info=Three_info,
												   four_info=Four_info,five_info=Five_info,six_info=Six_info}) ->
                      pack_every_times_info(Inning,util:to_list(One_info)++util:to_list(Two_info)++util:to_list(Three_info)++
                                            util:to_list(Four_info)++util:to_list(Five_info)++util:to_list(Six_info))
					end,
			  Everytimes_fighting_info = lists:map(Fun, List),
			  Pt =pt_sg_everytimes_fighting_info_d107:new(),
			  Pt1 =Pt#pt_sg_everytimes_fighting_info{everytimes_fighting_info=Everytimes_fighting_info},
			  ?send(Sid,pt_sg_everytimes_fighting_info_d107:to_binary(Pt1, 0));
		_ ->skip
	end.

pack_every_times_info(Inning,Everytimes_list) ->
	L = deserialize_every_times_info(Everytimes_list),
	Fun = fun({Aid,Name,Curscore,Stake,Cardlist,Dian_shu,Zhuangstate}) ->
			 Pt =pt_public_class:everytimes_fighting_info_new(),
				  F = fun({Color,Val}) -> 
							  Pt1 = pt_public_class:sg_card_list_new(),
							  Pt1#pt_public_sg_card_list{color=Color,val=Val}
					  end,					
              Pt#pt_public_everytimes_fighting_info{inning=Inning,aid=Aid,name=Name,score=Curscore,zhuang_state=Zhuangstate,
													xia_zhua_score=Stake,dian_shu=Dian_shu,card_list=lists:map(F, Cardlist)}
		  end,
	lists:map(Fun, L).

deserialize_every_times_info(Everytimes_list) ->
	F=fun(Str)->
			  [Aid,Name,Curscore,Stake,Cardlist,Dian_shu,Zhuangstate]=string:tokens(Str, "&"),					
			 list_to_tuple([util:to_integer(Aid),util:to_list(Name),util:to_integer(Curscore),util:to_integer(Stake),
                            util:to_list(Cardlist),util:to_integer(Dian_shu),util:to_integer(Zhuangstate)])
       end,
	lists:map(F, Everytimes_list).


